package com.foreveross.bsl.mam.model.application.impl;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.mam.model.application.WidgetSnapshotApplication;
import com.foreveross.bsl.mam.model.application.vo.WidgetSnapshotVO;
import com.foreveross.bsl.mam.model.model.domain.WidgetSnapshot;

@Named("widgetSnapshotApplication")
public class WidgetSnapshotApplicationImpl implements WidgetSnapshotApplication {

	@Override
	public WidgetSnapshotVO saveEntity(WidgetSnapshotVO entity) {
		// TODO Auto-generated method stub
		WidgetSnapshot wSnapshot = new WidgetSnapshot();
		BeanHelper.copyProperties(wSnapshot, entity);
		WidgetSnapshot snapshot = (WidgetSnapshot) wSnapshot.save();
		entity.setId(snapshot.getId());
		return entity;
	}

	@Override
	public WidgetSnapshotVO updateEntity(WidgetSnapshotVO entity) {
		// TODO Auto-generated method stub
		WidgetSnapshot wSnapshot = new WidgetSnapshot();
		BeanHelper.copyProperties(wSnapshot, entity);
		wSnapshot.save();
		return entity;
	}

	@Override
	public void deleteEntity(String id) {
		// TODO Auto-generated method stub
		WidgetSnapshot wSnapshot = new WidgetSnapshot();
		wSnapshot.delete(id);
	}

	@Override
	public WidgetSnapshotVO getEntity(String id) {
		// TODO Auto-generated method stub
		WidgetSnapshot wSnapshot = new WidgetSnapshot();
		wSnapshot = (WidgetSnapshot) wSnapshot.findOne(id);
		if (wSnapshot != null) {
			WidgetSnapshotVO entity = new WidgetSnapshotVO();
			BeanHelper.copyProperties(entity, wSnapshot);
			return entity;
		}
		return null;
	}

	@SuppressWarnings({"unchecked" })
	@Override
	public List<WidgetSnapshotVO> findByVersionId(String versionId) {
		// TODO Auto-generated method stub
		WidgetSnapshot wSnapshot = new WidgetSnapshot();
		List<WidgetSnapshot> list = wSnapshot.findListByProperty("versionId",
				OperatEnum.EQ, versionId);
		if (list != null && list.size() > 0) {
			List<WidgetSnapshotVO> result = new ArrayList<WidgetSnapshotVO>();
			WidgetSnapshotVO vo;
			for (int i = 0; i < list.size(); i++) {
				vo = new WidgetSnapshotVO();
				BeanHelper.copyProperties(vo, list.get(i));
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	@Override
	public List<WidgetSnapshotVO> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
